/*
Copyleft 2006 by T.S.Yo
*/

package exp.util;
import ec.Finisher;
import ec.EvolutionState;
import ec.util.Parameter;

/* 
 * MyExpFinisher.java
 * 
 * Created: 2007.01.11
 * By: T.S.Yo
 */

/**
 * MyExpFinisher is a replacement of SimpleFinisher which ouput the last 
 * solution population, in order to evaluate against same test-points.
 *
 * @author T.S.Yo
 * @version 1.0 
 */

public class MyExpFinisher extends Finisher
{

    public void setup(final EvolutionState state, final Parameter base) 
    { 
        //super.setup( state, base );
    }

    /* Write last generation of solution to file */
    public void finishPopulation(final EvolutionState state, final int result)
    {
        Parameter base = new Parameter("sol");
        String temp;
        //temp = state.parameters.getStringWithDefault(base.push("sol-file"),null,"final.solutions.log");
        temp = state.parameters.getString(base.push("file"),null);
        if( temp == null )
            state.output.fatal( "Parameter missing, output file name for final solution population.", base.push("file") );
        try
        {
            int log = state.output.addLog(new java.io.File(temp),0,false,false);
            // For each subpop
            for (int x=0;x<state.population.subpops.length;x++){
                for (int y=0;y<state.population.subpops[x].individuals.length;y++)
                {
                    ec.Individual i = state.population.subpops[x].individuals[y];
                    i.printIndividual(state,log,100);
                }
            }
            //for (int x=0;x<state.population.subpops[1].individuals.length;x++)
            //{
            //    ec.Individual i = state.population.subpops[1].individuals[x];
            //    i.printIndividual(state,log,100);
            //}
        }
        catch (java.io.IOException e) { 
            System.out.println("Can't open file "+temp+"!");
            System.out.println("Error: " + e);
        }
        return;
    }
}
